<?php
/**
 * Describe:后台会员类 包括登录 验证等
 * Source:
 * User: blue
 * Time: 2018/3/26 20:54
 */

namespace ocean;

use think\facade\Session;

class AdminLogin
{
    protected $config = [
        'session_info'=>'user_info',
        'session_sign'=>'user_sign',
        'session_expire'=>3600,
    ];

    public function __construct()
    {

    }

    /**
     * 数据签名认证
     * @param  array $post_data 被认证的数据
     * @return string       签名
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function user_sign($post_data)
    {
        //数据类型检测
        if (!is_array($post_data)) {
            $post_data = (array)$post_data;
        }
        ksort($post_data); //排序
        $code = http_build_query($post_data); //url编码并生成query字符串
        $sign = sha1($code); //生成签名
        return $sign;
    }
    /**
     * 登录模块
     * @return array
     */
    public function is_login()
    {
        //先判断session是不是存在
        $user = session($this->config['session_info']);
        if (empty($user)) {
            return ['status' => 0, 'msg' => '用户没有登录'];
        }
        //然后判断签名是否有效
        if (session($this->config['session_sign']) != $this->user_sign($user)) {
            return ['status' => 0, 'msg' => '签名不正常'];
        }
        //判断有效期是否有效,如超期删除,如不超期更新
        if (isset($user['expire'])) {
            //如果expire时间<当前时间,删除sesion,并且登出
            if ($user['expire'] < time()) {
                Session::delete($this->config['session_info']);
                Session::delete($this->config['session_sign']);
                return ['status' => 0, 'msg' => '登录已过期'];
            }
            //否则,更新expire时间,更新签名
            $user['expire'] = time() + $this->config['session_expire'];
            Session::set($this->config['session_info'], $user);
            Session::set($this->config['session_sign'], $this->user_sign($user));
        }
        return ['status' => 1, 'msg' => '登录成功'];
    }
    /**
     * 登录过程:包括用户名密码检测,以及写入session
     * @param array $post_data    登录提交数据
     * @param array $table_data   根据用户名查到的表的数据
     * @return array
     */
    public function login($post_data,$table_data){
            //判断验证码
            if(!captcha_check($post_data['code'])){
                return ['status'=>0,'msg'=>'验证码不正确'];
            };
            //判断用户名
            if(empty($table_data)) return ['status'=>0,'msg'=>'用户名不存在'];
            //验证密码
            $check=password_verify($post_data['password'],$table_data['password']);
            if(!$check) return ['status'=>0,'msg'=>'密码不正确'];
            //存入session中
            $sdata=[
                'id'=>$table_data['id'],
                'name'=>$table_data['mname'],
                'expire'=>time()+$this->config['session_expire'],
                ];
            session($this->config['session_info'],$sdata);
            session($this->config['session_sign'],$this->user_sign($sdata));
            return ['status'=>1,'msg'=>'登录成功'];
    }
    /**
     * 用户注销
     */
    public function logout(){
        Session::delete($this->config['session_info']);
        Session::delete($this->config['session_sign']);
        return ['status'=>1,'msg'=>'注销成功'];
    }
    /*
     * 修改密码
     */
    public function changPWD(){

    }

}